package com.quzi.kettle.activitise.added;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.quzi.kettle.R;
import com.quzi.kettle.activitise.BaseActivity;
import com.quzi.kettle.view.TitleBarView;

public class VoiceRecordActivity extends BaseActivity {

	private TitleBarView titlebar;
	private Handler handler;
	private RelativeLayout rela_voicerecord_secord;
	private TextView tv_voicerecord_secord;
	private Boolean ifpresse = false;
	private int TotalSecord = 30;
	private final static int PRESSING = 1;
	private final static int PRESSUP = 2;
	private final static int PLAY = 3;
	private final static int TIME = 1000;
	private Button bt_voicerecord_pressrecord;
	VoiceRecordUtil vru = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_voicerecord);
		inittitlebar();
		initRecordButton();
		vru = new VoiceRecordUtil();
		handler = new Handler() {
			@Override
			public void dispatchMessage(Message msg) {
				super.dispatchMessage(msg);
				switch (msg.what) {
				case PRESSING:
					TotalSecord -= 1;
					if (TotalSecord <= 0) {
						handler.sendEmptyMessageDelayed(PRESSUP, TIME);
						return;
					} else {
						handler.sendEmptyMessageDelayed(PRESSING, TIME);
					}
					if (!ifpresse) {
						VoiceRecordUtil.startRecord();
					}
					ifpresse = true;
					tv_voicerecord_secord.setText(TotalSecord + "");
					bt_voicerecord_pressrecord.setBackground(getResources()
							.getDrawable(R.drawable.shape_rect_grey));

					break;
				case PRESSUP:
					ifpresse = false;
					VoiceRecordUtil.stopRecord();
					bt_voicerecord_pressrecord.setBackground(getResources()
							.getDrawable(R.drawable.shape_rect_pink));
					TotalSecord = 30;
					tv_voicerecord_secord.setText(TotalSecord + "");
					handler.removeMessages(PRESSING);
					showTextToast("录音成功");
					handler.sendEmptyMessageDelayed(PLAY, 500);
					// VoiceRecordUtil.startPlay();
					break;
				case PLAY:
					VoiceRecordUtil.startPlay(getApplicationContext());
					break;
				default:
					break;
				}
			}
		};

	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		VoiceRecordUtil.Destory();
	}

	private void inittitlebar() {
		titlebar = (TitleBarView) findViewById(R.id.titleview);
		titlebar.setTitle(getResources().getString(R.string.voicerecord));
	}

	private void initRecordButton() {
		rela_voicerecord_secord = (RelativeLayout) findViewById(R.id.rela_voicerecord_secord);
		bt_voicerecord_pressrecord = (Button) findViewById(R.id.bt_voicerecord_pressrecord);
		tv_voicerecord_secord = (TextView) findViewById(R.id.tv_voicerecord_secord);
		bt_voicerecord_pressrecord.setOnTouchListener(new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				if (event.getAction() == MotionEvent.ACTION_DOWN) {
					Log.v("test", "ACTION_DOWN");
					if (TotalSecord > 0) {

						handler.sendEmptyMessageDelayed(PRESSING, TIME);

					} else {

						handler.sendEmptyMessageDelayed(PRESSUP, TIME);
					}
				} else if (event.getAction() == MotionEvent.ACTION_UP) {
					Log.v("test", "ACTION_UP");

					handler.sendEmptyMessage(PRESSUP);

				}
				return false;
			}
		});
	}

	@Override
	public void onClick(View arg0) {

	}

}
